package network.api;

import org.junit.Test;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author mubei
 * @date 2021/12/5
 * @description
 */
public class Test01 {

    /*
    * 发送端
    * */
    @Test()
    public void test1() throws IOException {
        Socket socket = new Socket("127.0.0.1", 9999);
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("阿慧真漂亮".getBytes());
        // 设置结束标记
        socket.shutdownOutput();
        InputStream is = socket.getInputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int length = 0;
        byte bys[] = new byte[1024];

        while((length = is.read(bys))!=-1){
            baos.write(bys,0,length);
        }
        System.out.println(baos.toString());
        outputStream.close();
        socket.close();
    }

    /*
    * 接受端
    * */
    @Test()
    public void test2() throws Exception {

        //  监听服务
        ServerSocket serverSocket = new ServerSocket(9999);
        // 创建 socket
        Socket accept = serverSocket.accept();

        InputStream is = accept.getInputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int length = 0;
        byte bys[] = new byte[1024];

        while((length = is.read(bys))!=-1){
            baos.write(bys,0,length);
        }
        System.out.println(baos.toString());

        OutputStream outputStream = accept.getOutputStream();
        outputStream.write("那肯定是的".getBytes());
        // 设置结束标记
        accept.shutdownOutput();
        is.close();
        baos.close();
        accept.close();
    }
    

    /*
    * 实现文件下载
    * */
    @Test()
    public void testServer() throws Exception {
        String path = "D:\\date\\distable\\practice\\code\\basic\\src\\main\\java\\network\\api/sg2.jpg";
        ServerSocket serverSocket = new ServerSocket(9999);
        Socket socket = serverSocket.accept();
        InputStream inputStream = socket.getInputStream();
        BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(path));

        int len = 0;
        byte bys[] = new byte[1024];
        while((len = inputStream.read(bys))!=-1){
            outputStream.write(bys,0,len);
        }
        outputStream.flush();

        OutputStream socketOutputStream = socket.getOutputStream();
        socketOutputStream.write("ah真漂亮".getBytes());
        socket.shutdownOutput();

        socketOutputStream.close();
        outputStream.close();
        inputStream.close();
        socket.close();
    }

    /*
     * 实现文件上传
     * */
    @Test()
    public void testClient() throws Exception {
        String path = "D:\\date\\distable\\practice\\code\\basic\\src\\main\\java\\network\\api/sg.jpg";
        Socket socket = new Socket("127.0.0.1", 9999);


        OutputStream outputStream = socket.getOutputStream();
        FileInputStream inputStream = new FileInputStream(path);
        byte bys[] = new byte[1024];
        int len = 0;

        while((len = inputStream.read(bys))!= -1){
                outputStream.write(bys,0,len);
        }
        // 设置结束标记
        socket.shutdownOutput();
        InputStream stream = socket.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
        System.out.println(reader.readLine());

        inputStream.close();
        outputStream.close();
        reader.close();
        socket.close();


    }
    
}
